#获取文件大小
#递归
import os


def get_dirsize(dirpath):
    size = 0
    #os.walk: 获取指定路径下的所有文件，包括子路径下的所有文件都会获取到
    for dirname,subdirs,files in os.walk(dirpath):
        #dirname:当前文件夹的路径
        #dirs:当前文件夹的子路径
        #files:当前文件夹中的文件
        
        for file in files:
            filepath = os.path.join(dirname,file)
            size += os.path.getsize(filepath)
    #格式化单位
    size_text = ""
    if size < 1024:
        size_text = "%.2f字节"%size
    elif size>=1024 and size<1024*1024:
        size_text = "%.2fkb"%(size/1024)
    elif size>1024*1024 and size<1024*1024*1024:
        size_text = "%.2fM"%(size/1024/1024)
    elif size_text >1024*1024*1024 and size< 1024*1024*1024*1024:
        size_text = "%.2fG"%(size/1024/1024/1024)
    print(size_text)   

def main():
    dirpath = input("请输入路径： ")
    size = get_dirsize(dirpath)
    print(size)

if __name__ == "_main_":
    main()
